package com.meiqiongproperty.yiguanjia.fragment.home.yipension;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.activity.GenericFragmentActivity;
import com.meiqiongproperty.yiguanjia.databinding.MonthlyComboLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnItemClickListener;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.yiPensionAdapter.MonthlyComboListAdapter;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendFreshFruitList;
import com.meiqiongproperty.yiguanjia.httpbean.pension.MonthlyComboBean;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: Administrator
 * date: 2017/5/13.
 * package:com.meiqiongproperty.yiguanjia.fragment.home.yipension
 */

public class MonthlyComboFragment extends BaseFragment {
    MonthlyComboLayoutBinding binding;
    List<MonthlyComboBean> monthlyComboList=new ArrayList<>();
    MonthlyComboListAdapter adapter;
    Handler handler=new Handler();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding= DataBindingUtil.inflate(inflater, R.layout.monthly_combo_layout,container,false);
        initDate();

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerMonthlyCombo.setLayoutManager(manager);
        getMonthList();

        return binding.getRoot();
    }


    private void initDate(){
        binding.navigation.setTitle("包月套餐");
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
    }

    /**
     * 包月套餐
     */
    public void getMonthList(){
        SendFreshFruitList bean = new SendFreshFruitList();
        bean.setToken(Utils.getToken(getActivity()));
        bean.setFirstCate("3");
        ApiService.getPensionMonthList(getActivity(), new IHttpCallback<MonthlyComboBean>() {
            @Override
            public void onNext(final MonthlyComboBean monthlyComboBean) {
                adapter=new MonthlyComboListAdapter(getActivity(),monthlyComboBean.getShoplist());
                binding.recyclerMonthlyCombo.setAdapter(adapter);

                adapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Bundle bundle=new Bundle();
                        bundle.putString("productId",monthlyComboBean.getShoplist().get(position).getProductId());
                        GenericFragmentActivity.start(getActivity(),DailyCareDetailFragment.class,bundle);
                    }
                });
            }

            @Override
            public void onError(int code, String message) {

            }
        }, Utils.getRequestBody(bean));
    }

    @Override
    public void onResume() {
        super.onResume();
        getMonthList();
    }
}
